1176A - Divide it - CodeForces Solution


brute force greedy implementation *800

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    n=int(input())
    k=0
    while n>1:
        if n%2==0:
            n=n//2
            k+=1
  
    
        elif n%3==0:
            n=2*(n//3)
            k+=1

        elif n%5==0:
            n=4*(n//5)
            k+=1
        else:
            k=-1
            break
    print(k)

C++ Code:

// Author : abdelrhman
#include <bits/stdc++.h>
#define forLoop(size) for (size_t i = 0; i < (size); i++)
#define forReverse(size) for (size_t i = (size - 1); i >= 0; i--)
#define forE(v) for (auto k : v)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fj(size) for (size_t j = 0; j < (size); j++)
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define InputOutput                   \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr);
#define ll long long
#define l long
#define EL cout << "\n";
#define YES cout << "YES" \
                 << "\n";
#define NO cout << "NO" \
                << "\n";
using namespace std;
template<typename T>
ostream & operator <<(ostream& os,deque<T>v){
    forE(v){
    cout << k << " ";
    }
    return os;
}
template<typename P>
ostream & operator <<(ostream& os,vector<P>v){
    forE(v){
    cout << k << " ";
    }
    return os;
}
/*
bool sortbysec( pair<ll,ll> a,pair<ll,ll> b)
{
    return (a.second > b.second);
}
*/
bool sortbyFirst( pair<ll,ll> a,pair<ll,ll> b)
{
    return(b.first > a.first);
}
/*void subString(string s)
{
    int n = s.size();
    for (int i = 0; i < n; i++)
        for (int len = 1; len <= n - i; len++)
            //subStr[s.substr(i, len)]++;
}*/

/*
17 27
-257 -320 -676 -1136 -2068 2505 2639 4225 4951 5786 7677 7697 7851 8337 8429 8469 9343

*/
void fun()
{
    ll n, counter = 0;
    cin >> n;
    while(n != 1)
        {
            if(n % 2 != 0&& n % 3 != 0 && n % 5 != 0){cout << -1 << endl;return;}
            vector<ll>v;
            if(n % 2 == 0)v.push_back(n/2);
            if(n % 3 == 0)v.push_back((n*2)/3);
            if(n % 5 == 0)v.push_back((4*n)/5);
            sort(all(v));
            n = v[0];
            counter++;
        }
    cout << counter << endl;
}
int main()
{
    fast
    InputOutput
    int t = 1;
   cin >> t;
    while(t--)
        {
            fun();
        }
}


Comments

Submit
0 Comments
More Questions

739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber